<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------

namespace app\services\car;


use app\dao\car\BaseCityDao;
use app\model\car\BaseCity;
use app\services\BaseServices;
use crmeb\exceptions\AdminException;
use crmeb\services\FormBuilder as Form;
use think\exception\ValidateException;
use think\facade\Route as Url;

/**
 * Class BaseCityServices
 * @package app\services\shendeng
 */
class BaseCityServices extends BaseServices
{
    public function __construct(BaseCityDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 列表
     * @param array $where
     * @param bool $is_page
     * @return array
     */
    public function getList(array $where = [],bool $is_page = false){
        $page = $limit = 0;
        if($is_page){
            [$page, $limit] = $this->getPageValue();
        }
        $list = $this->dao->getList($where,$page,$limit);
        return $list;
    }

    /**
     * 获取城市列表
     * @param array $where
     * @param bool $is_page
     * @return array
     * @throws \ReflectionException
     */
    public function getCityList(array $where = [],bool $is_page = false){
        $page = $limit = 0;
        if($is_page){
            [$page, $limit] = $this->getPageValue();
        }
        $list = $this->dao->getList($where,$page,$limit);
        $city = array();
        foreach ($list['list'] as $key => $item){
            $city[$key]['value'] = $item['id'];
            $city[$key]['label'] = $item['city_name'];
        }
        $count = $this->dao->count($where);
        return compact('city','count');
    }

    /**
     * 添加/修改
     * @param int $id
     * @return array
     * @throws \FormBuilder\Exception\FormBuilderException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add(int $id){
        $city = $this->dao->get($id);
        $field = array();
        $title = $id ? '修改' : '添加';
        $field[] = Form::hidden('id', $id);
        $field[] = Form::input('city_name', '城市名称', $city['city_name']??'');
        $field[] = Form::input('short_name', '城市简称', $city['short_name']??'');
        $field[] = Form::radio('status','是否显示',$city['status']??1)->options([['label'=>'显示','value'=>1],['label'=>'隐藏','value'=>0]]);
        $field[] = Form::number('sort', '排序', $city['sort']??0);
        return create_form($title, $field, Url::buildUrl('/base/city/save'), 'POST');
    }

    /**
     * 添加/修改保存
     * @param int $id
     * @param array $data
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save(int $id, array $data){
        $city = $this->dao->getOne(['city_name' => $data['city_name'],'is_del'=>0]);
        if ($id) {
            if (!$this->dao->get($id)) throw new AdminException('数据不存在');
            if ($city && $id != $city['id']) throw new AdminException('该城市已经存在');
            if (!$this->dao->update($id, $data)) throw new AdminException('修改失败或者您没有修改什么');
        } else {
            unset($data['id']);
            if ($city) throw new AdminException('该城市已经存在');
            $data['add_time'] = time();
            if (!$this->dao->save($data)) throw new AdminException('添加失败');
        }
        return true;
    }

    /**
     * 删除
     * @param int $id
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delCity(int $id){
        $city = $this->dao->get($id);
        if ($city) {
            if (!$this->dao->update($id,['is_del'=>1,'delete_time'=>time()])) throw new AdminException('删除失败,请稍候再试!');
        }
        return '删除成功!';
    }

    /**
     * 显示/隐藏
     * @param int $id
     * @param int $status
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function setShow(int $id, int $status){
        $city = $this->dao->getOne(['id' => $id,'is_del'=>0]);
        if (!$city) throw new AdminException('数据不存在');
        if ($this->dao->update($id, ['status' => $status])) {
            return $status == 1 ? '显示成功' : '隐藏成功';
        } else {
            throw new AdminException($status == 1 ? '显示失败' : '隐藏失败');
        }
    }

    /**
     * 获取所有显示的城市
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAllCity(){
        $list = BaseCity::where(['is_del'=>0,'status'=>1])->select()->toArray();
        $city = [];
        foreach ($list as $item){
            $city[] = ['value'=>$item['id'],'label'=>$item['city_name']];
        }
        return $city;
    }

    /**
     * 获取所有显示的城市2
     * 2023年12月1日13:56:22
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAllCity2(){
        $list = BaseCity::where(['is_del'=>0,'status'=>1])->select()->toArray();
        $city = [];
        foreach ($list as $item){
            $city[] = ['id'=>$item['id'],'name'=>$item['city_name']];
        }
        return $city;
    }


    /**
     * 判断城市是否开通
     * @param $data
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function isCityOpen($data){
        $city = BaseCity::where('city_name','like',"%${data['city_name']}%")->where(['status'=>1,'is_del'=>0])->find();
        $city = $city ? $city->toArray() : [];
        if(!$city) throw new ValidateException('城市未开通，请选择其他城市');
        return $city;
    }
}
